package com.suray.wcs.service.pojo.rgv.rgv.protocol.message;

import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.wcs.res.enums.Order;
import com.suray.wcs.service.pojo.Rgv;
import com.suray.basic.wcs.utils.typedef.Bit;
import com.suray.basic.wcs.utils.typedef.Byte;
import com.suray.basic.wcs.utils.typedef.DWord;
import com.suray.basic.wcs.utils.typedef.Word;
import com.suray.wcs.service.pojo.rgv.action.RgvCommandAction;
import com.suray.wcs.service.pojo.rgv.action.util.RgvActionStatus;
import com.suray.wcs.service.pojo.rgv.rgv.protocol.base.RgvSendMessage;
import com.suray.wcs.service.pojo.rgv.rgv.protocol.util.RgvMessageType;

public class RgvDebugCmdSendMessage extends RgvSendMessage {
    public RgvDebugCmdSendMessage(Rgv rgv, int cmdNo, int cmd, int secondaryCmd, int cmdParam) {
        super(rgv, "指令发送");
        baseDatas.add(new Word("firstFrame", "报文头帧：02 fd"));
        baseDatas.add(new Byte("deviceId", "小车对应设备号"));
        baseDatas.add(new Byte("life", "心跳、指令、任务共用，没发一帧数据+1,0-255循环，res收到后原数据返回"));
        baseDatas.add(new Bit(4,"frameType", "报文类型"));
        baseDatas.add(new Bit(4,"version", "版本号"));
        baseDatas.add(new Byte("taskNo", "任务序号"));
        baseDatas.add(new Byte("cmdNo", "操作指令序号"));
        baseDatas.add(new Byte("cmd", "指令ID"));
        baseDatas.add(new Byte("secondaryCmd", "次指令ID"));
        baseDatas.add(new DWord("cmdParam", "指令参数"));
        baseDatas.add(new Word("msgLen", "长度"));
        baseDatas.add(new Word("CRC16", "CRC检验位"));
        baseDatas.add(new Word("endFrame", "报文尾帧：03 fc"));

        setDefaultValue();
        setValue("frameType", RgvMessageType.RGV_DEBUG_CMD_MESSAGE.getValue());
        setValue("cmdNo", cmdNo);
        setValue("cmd", cmd);
        setValue("secondaryCmd", secondaryCmd);
        setValue("cmdParam", cmdParam);

        LoggerUtil.debug(toString(), this.getClass());
    }

    public int getTaskNo() {
        return getValue("taskNo");
    }

    public int getCmdNo() {
        return getValue("cmdNo");
    }

    public int getCmd() {
        return getValue("cmd");
    }

    public int getCmdParam() {
        return getValue("cmdParam");
    }

    public int getSecondaryCmd() {
        return getValue("secondaryCmd");
    }

    public void analyzeCommandMessage() {
        Rgv rgv = getRgv();
        RgvCommandAction action = rgv.getCmdAction();

        action.setActionNo(getCmdNo());
        action.setTaskNo(getTaskNo());
        action.setActionType(Order.parseOrder(getCmd()));
        action.setActionStatus(RgvActionStatus.RGV_WAITING_ACTION);
        action.setCmdParam(getCmdParam());
    }

    @Override
    protected void setCrc(int crc)  {
        setValue("CRC16", crc);
    }

    @Override
    protected void setLength(int length) {
        setValue("msgLen", length);
    }

    @Override
    public Integer getMessageLife() {
        return getValue("life");
    }

    @Override
    public Integer getMessageType() {
        return getValue("frameType");
    }


}
